﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace HmLab.Func1;

/// <summary>
/// <see cref="LbLoadHualalaData"/>
/// </summary>
[Obsolete]
internal class ExportDataMerger {

  public HuallLoadResult Load(string path, string group, AppConfigRoot config) {
    DataRow[] dataList = ReadDataRows(path, group, config);

    HuallMergeRow[] resultList = dataList
      .GroupBy(r => r.GroupKey)
      .Select(g => MergeRowGroup([.. g]))
      .ToArray();

    string name = Path.GetFileNameWithoutExtension(path);
    return new(name, resultList);
  }

  record DataRow(DataHeaderCfg[] Header, string GroupKey, object[] ColVal);
  DataRow[] ReadDataRows(string path, string group, AppConfigRoot config) {
    DataHeaderCfg[] headerCfg = config.Header[group];
    string[] readCol = headerCfg
      .Where(h => h.Func != HeaderColFunc.VLOOKUP)
      .Where(h => h.Func != HeaderColFunc.COUNT)
      .Select(h => h.Name)
      .ToArray();

    LgExcelResult excelResult = _excelReader.Read(path, 0, "3", readCol);
    Dictionary<string, int> headerCol = excelResult.HeaderCol;

    int[] keyCol = headerCfg
      .Where(h => h.Func == HeaderColFunc.KEY)
      .Select(h => headerCol[h.Name])
      .ToArray();

    var getCellVal = (DataHeaderCfg header, int col, LgExcelRow row) => {
      if (header.Func == HeaderColFunc.COUNT) {
        return 1;
      }

      object cell = row.ColVal[headerCol[header.Name]];
      if (header.Func == HeaderColFunc.VLOOKUP) {
        string key = (string)cell;
        return config.Lookup[key];
      }
      return cell;
    };

    var makeRow = (LgExcelRow row) => {
      object[] colList = headerCfg
        .Select((h, c) => getCellVal(h, c, row))
        .ToArray();

      string key = string.Join('_', keyCol
        .Select(c => row.ColVal[c])
        .ToArray());

      return new DataRow(headerCfg, key, colList);
    };

    LgExcelRow lastRow = excelResult.Rows[^1];
    bool lastRowHasData = !string.IsNullOrEmpty((string)lastRow.ColVal[1]);
    return (lastRowHasData ? excelResult.Rows : excelResult.Rows[..^1])
      .Select(makeRow)
      .ToArray();
  }

  HuallMergeRow MergeRowGroup(DataRow[] rowList) {
    DataRow groupInfo = rowList[0];
    DataHeaderCfg[] headerCfg = groupInfo.Header;

    //Console.WriteLine("ggggggggggroupkey----- " + groupInfo.GroupKey);

    var round = (double d) => Math.Round(d, 2);
    var makeCell = (DataHeaderCfg header, int col) => {
      if (header.Func == HeaderColFunc.COUNT) {
        return rowList
           .Select(r => (int)r.ColVal[col])
           .Sum();
      }

      if (header.Func == HeaderColFunc.SUM) {
        return round(rowList
           .Select(r => (double)r.ColVal[col])
           .Sum());
      }
      return groupInfo.ColVal[col];
    };

    return new(headerCfg
      .Select((h, i) => makeCell(h, i))
      .ToArray());
  }

  readonly LargeExcelReaderLb _excelReader = new();
}
